<template>
	<view class="manage">

		<block v-if="total">
			<view class="all-address">
				<view v-for="(item,index) in list" :key="index">

					<view class="border" @click="selectAddress(item)">
						<view class="flex-left-right" style="padding-top: 30rpx;">
							<text class="fs-18 bold">{{item.shou_name}}</text>
							<text>{{item.shou_phone}}</text>
						</view>
						<view style="padding:20rpx 0">
							
							{{item.province+item.city+item.area+item.address}}
						</view>
					</view>
					<view class="select">
						<view class="select-default " @click="setDefault(item,index)">
							
							<uni-icons :type="item.is_check==1?'circle':'checkbox-filled'" size="20"
								:color="item.is_check==1?'black':'#FF7133'"></uni-icons>
							<text>设为默认地址</text>
						</view>
						<view class="operate">
							<text @click="edit(item)">编辑</text>
							<text @click="deleteAddress(item.id,index)">删除</text>
						</view>
					</view>

				</view>
			</view>
			<view style="height: 100rpx;"></view>
			<view class="fixed-style" :class="[system=='ios'?'pm':'']">
				<view class="c-button" @click="add">添加新地址</view>
			</view>
		</block>
		<block v-else>
			<view class="default-no">
				<image src="../../static/img/no_content.png" mode="aspectFit"></image>
				<text>暂时没有用餐地址哦~</text>
				<view @click="add"> 添加地址</view>
			</view>

		</block>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				total: 0,
				system:''
			}
		},
		methods: {
			deleteAddress(id, index) {
				this.$showModal('确定要删除该地址吗', () => {
					let params = {
						id: id
					}
					this.$get(this.$api.aDel, params, (res) => {
						let {
							data
						} = res;
						if (data.code == 1) {
						this.getAddress()
							this.$Toast('删除成功');
						} else {
							this.$Toast(data.msg);
						}
					})
				})
			},
			edit(item) {
				// console.log(item)
				uni.navigateTo({
					url: './edit?item=' + JSON.stringify(item)
				})
			},
			add() {
				uni.navigateTo({
					url: './add'
				})
			},
			getAddress() {
				let params = {
					token: uni.getStorageSync('userToken'),
					page: this.page,
					limit: 1000
				}
				this.$get(this.$api.aMy, params, (res) => {
					let {
						data
					} = res;
					if (data.code == 1) {
						this.list = data.data.data;
						this.total = data.data.total;
					}
				})
			},
			setDefault(data, index) {
				for (let i in this.list) {
					this.list[i].is_check = 1;
				}
				this.list[index].is_check = 2;
				let params = {
					id: data.id
				}
				this.$get(this.$api.aCheck, params, (res) => {
					let {
						data
					} = res;
					if (data.code == 1) {
						this.$Toast('设为默认地址成功');
						// uni.setStorageSync('address', [this.list[index]]);
					}
				})
			},
			selectAddress(item) {
				uni.setStorageSync('address', JSON.stringify(item));
				this.$back();
			}
		},
		onShow() {
			this.getAddress();
			this.system=uni.getSystemInfoSync().platform;
		}

	}
</script>

<style lang="scss" scoped>
	.pm{
		padding-bottom: 45rpx !important;
	}
	.manage {
		min-height: 100vh;
		display: flex;
		flex-direction: column;

		.all-address>view {
			background: white;
			padding: 0 30rpx;
			margin-bottom: 16rpx;

		}

		.select {
			height: 96rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.select-default {
			display: flex;
			align-items: center;

			text {
				margin-left: 10rpx;
			}
		}

		.operate text {
			width: 100rpx;
			height: 40rpx;
			line-height: 40rpx;
			margin-left: 31rpx;
			border: 1px solid #ccc;
			border-radius: 20rpx;
			display: inline-block;
			font-size: 24rpx;
			text-align: center;
			box-sizing: border-box;
		}

		.fixed {
			position: fixed;
			bottom: 0;
			background: white;
			padding: 10rpx 0;
			width: 100%;

			.custom {
				width: 630rpx;
			}
		}

		.default-no {
			// height: calc(100vh - 140rpx);
			background: white;
			display: flex;
			flex: 1;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			text {
				margin:0rpx 0 29rpx;
				color:gray;
				font-size: 26rpx;
			}

			image {
				width: 456rpx;
				margin-top: -100rpx;
			}

			view {
				background: $m-c;
				color: white;
				height: 60rpx;
				width: 220rpx;
				text-align: center;
				line-height: 60rpx;
				border-radius: 30rpx;
			}
		}
	}

	.fixed-style {
		position: fixed;
		bottom: 0;
		padding-top: 10rpx;
		padding-bottom: 10rpx;
		width: 100%;
		border-top: 1px solid #eee;
		background-color: white;
		z-index: 9;

		.c-button {
			display: flex;
			justify-content: center;
			align-items: center;
			color: white;
			border-radius: 39rpx;
			margin: 0 auto;
			background: $m-c;
			height: 78rpx;
			width: 690rpx;
			margin: 0 auto;
		}
	}
</style>
